tyNamespace.TileCardNum = ["J", "Q", "K", "A", "2", "2", "1"];

tyNamespace.Tile = cc.Class.extend({
    type: null,
    typeValue: 0,
    num: null,
    cardNum: null,
    img: null,
    index: 0,
    status: tyNamespace.TileStatus.Li,
    sprite: null,
    spriteRemoved: false,
    belong: null,
    clickTime: 0,
    id: 0,
    realTile: null,
    isDiZhu: false,

    orderIndex: tyNamespace.OrderIndex.ZiJi,

    ctor: function (typeValue, num) {
        this.type = tyNamespace.tileTypes[typeValue];
        this.typeValue = typeValue;
        this.num = num;
        this.cardNum = num + 2;
        if (this.cardNum > 10) {
            var yu = this.cardNum % 10;
            this.cardNum = tyNamespace.TileCardNum[yu - 1];
        }

        //this.id = tyNamespace.guid();
        this.realTile = this;

        this.generateImg();
    },

    removeFromParent: function () {
        if (this.sprite != null) {
            cc.log(this.sprite + "remove");
            this.sprite.removeFromParent();
            this.sprite = null;
        }

        this.spriteRemoved = true;
    },

    setBelong: function (belong) {
        this.belong = belong;
        this.orderIndex = belong.orderIndex;

        if (belong.playLayer)
            this.isDiZhu = belong.playLayer.table.zhuangIndex == this.orderIndex;

        this.generateImg();
    },

    setStatus: function (status) {
        this.status = status;
        this.generateImg();
    },

    getImg: function () {
        return this.img;
    },

    generateImg: function () {
        if (this.orderIndex != tyNamespace.OrderIndex.ZiJi && this.status == tyNamespace.TileStatus.Li) {
            this.img = "#x_0back.png";
            return;
        }

        if (this.status == tyNamespace.TileStatus.Chi) {
            this.img = "#d_0back.png";
            return;
        }

        var pre = this.type;

        //cc.log("this.isDiZhu:" + this.isDiZhu + "player:" + this.belong.userItem.nickName);

        if (this.status == tyNamespace.TileStatus.Da)
            pre = "x" + pre;

        pre += "_0" + this.cardNum + ".png";

        this.img = "#" + pre;

//        cc.log("this.img: " + this.img);
    },

    getCopy: function (belong, offset) {
        if (belong == null)
            belong = this.belong;

        if (offset == undefined)
            offset = 0;

        var copy = new tyNamespace.Tile(this.typeValue, this.num + offset);

        if (belong != null)
            copy.setBelong(belong);

        return copy;
    }
});